package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.MathUtils;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class TanNhiThe extends MinisterEnemy{
	private static final long[] durationMove = {80, 80, 80, 80, 80, 80, 80, 80, 80};
	private static final long[] durationShoot = {200, 200, 250, 250, 300, 200};
	private static final long[] durationDie = {150, 200, 250, 300, 350};
	
	private boolean isAttachArrow = false;
	private boolean isShoot = false;
	private boolean isShootChecked = false;
	private boolean isAttackChecked = false;
	private boolean isMove = false;
	
	private StatesTanNhiThe mState;
	
	public TanNhiThe(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.setHeight(pTiledTextureRegion.getTileHeight() + 20);
		this.setHeight(pTiledTextureRegion.getTileWidth() + 15);
		this.name = Constants.TANVUONG;
		mState = StatesTanNhiThe.MOVE;
		animate(durationMove, 0, 8, true);
		handler.setVelocityX( - Constants.VELOCITY_TAN_VUONG_DEFAULT);
	}
	
	public void onManagedUpdate(float pSecondsElapsed) {
		if (checkShoot())
			shoot();
		else if (checkDie())
			die();
		if (isMove && mState != StatesTanNhiThe.MOVE) {
			move();
			isMove = false;
		}
		if (mState == StatesTanNhiThe.DIE) {
			setAlpha(getAlpha() - 0.02f);
		} else if (mState == StatesTanNhiThe.SHOOT) {
			if (getCurrentTileIndex() == 10) {
				if (!isAttackChecked) {
					isAttachArrow = true;
					isAttackChecked = true;
				}
			} else if (getCurrentTileIndex() == 14) {
				if (!isShootChecked) {
					isShoot = true;
					isShootChecked = true;
				}
			} else {
				isAttackChecked = false;
				isShootChecked = false;
			}
		}
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	public StatesTanNhiThe getState() {
		return mState;
	}
	
	public void stopShoot() {
		isShoot = false;
	}
	
	public void stopAttachArrow() {
		isAttachArrow = false;
	}
	
	private boolean checkShoot() {
		if (getX() <= Constants.XPOS_ATTACK_TANNHITHE && (mState == StatesTanNhiThe.MOVE || mState == StatesTanNhiThe.SHIRK) && mState != StatesTanNhiThe.SHOOT)
				return true;
		return false;
	}
	
	public boolean isAttachArrow() {
		return isAttachArrow;
	}
	
	public boolean isShoot() {
		return isShoot;
	}
	
	private boolean checkDie() {
		if (getHP() < 0 && mState != StatesTanNhiThe.DIE)
			return true;
		return false;
	}
	
	public void setMove() {
		this.isMove = true;
	}
	
	public void shirk() {
		float pX = MathUtils.random(Constants.XPOS_ATTACK_TANNHITHE, 750);
		float pY = MathUtils.random(50, 300);
		
		float offSet = (float)(Math.sqrt(pX * pX + pY * pY));
		
		stopAnimation();
		mState = StatesTanNhiThe.SHIRK;
		
		MoveModifier modifier = new MoveModifier(150 / offSet, this.getX(), pX, getY(), pY);
		registerEntityModifier(modifier);
	}
	
	public void move() {
		animate(durationMove, 0, 8, true);
		registerUpdateHandler(handler);
		mState = StatesTanNhiThe.MOVE;
	}
	
	public void shoot() {
		unregisterUpdateHandler(handler);
		animate(durationShoot, 9, 14, true);
		mState = StatesTanNhiThe.SHOOT;
	}
	
	public void die() {
		clearUpdateHandlers();
		clearEntityModifiers();
		animate(durationDie, 15, 19, true);
		mState = StatesTanNhiThe.DIE;
	}
	
	public static enum StatesTanNhiThe {
		MOVE, DIE, SHOOT, SHIRK
	}
	
}
